package logmgr;


public class LogReader {
	public static void show ()  {
		long old =0 ; 
		try {
			old= LogMgr.file.getFilePointer() ; 
			LogMgr.file.seek( 4 ) ;
			LogMgr rec = null ;
			while ( (rec = LogMgr.getNext() ) != null ) {
				rec.read(); 
				if ( rec instanceof IntRecord )System.out.printf( "WriteInt: Tx:%d Page:%d offset:%d oldVale:%d newVal:%d\n",
					 rec.txnum, rec.pageid, rec.pos, ((IntRecord)rec).old, ((IntRecord)rec).cur ) ;
				else if ( rec instanceof CharRecord )System.out.printf( "WriteChar: Tx:%d Page:%d offset:%d oldVale:%c newVal:%c\n",
						 rec.txnum, rec.pageid, rec.pos, ((CharRecord)rec).old, ((CharRecord)rec).cur ) ;
				else if ( rec instanceof StringRecord )System.out.printf( "WriteString: Tx:%d Page:%d offset:%d oldVale:%s newVal:%s\n",
						 rec.txnum, rec.pageid, rec.pos, ((StringRecord)rec).old, ((StringRecord)rec).cur ) ;
				else if ( rec instanceof FloatRecord )System.out.printf( "WriteFloat: Tx:%d Page:%d offset:%d oldVale:%f newVal:%f\n",
						 rec.txnum, rec.pageid, rec.pos, ((FloatRecord)rec).old, ((FloatRecord)rec).cur ) ;
				else if ( rec instanceof ShortRecord )System.out.printf( "WriteShort: Tx:%d Page:%d offset:%d oldVale:%d newVal:%d\n",
						 rec.txnum, rec.pageid, rec.pos, ((ShortRecord)rec).old, ((ShortRecord)rec).cur ) ;
				else if ( rec instanceof BytesRecord ) showBytes (rec ); 
				else if ( rec instanceof StartRecord ) System.out.printf ( "START Tx:%d\n" , rec.txnum ) ;
				else if ( rec instanceof RollbackRecord ) System.out.printf( "ROLLBACK Tx:%d\n" , rec.txnum ) ;
				else if ( rec instanceof CommitRecord ) System.out.printf( "COMMIT Tx:%d\n", rec.txnum) ;
			}
			LogMgr.file.seek( old ) ;
		}catch ( Exception e ) {
			e.printStackTrace () ;
		}
	}
	
	public static void showBytes ( LogMgr rec ) {
		System.out.printf( "WriteBytes: Tx:%d Page:%d offset:%d\n",
				rec.txnum, rec.pageid, rec.pos) ;
		byte [] old =  ((BytesRecord)rec).old ;
		byte [] cur = ((BytesRecord)rec).cur ; 
		System.out.print ( "Old Value:") ; 
		for ( int i = 0 ; i < old.length ; i ++) {
			if ( cur [ i  ] < 16 ) System.out.printf ( " 0%H", old [ i ] ) ;
			else System.out.printf ( " %H", old [ i ] ) ;
		}
		System.out.print ( "\nNew Value:") ; 
		for ( int i = 0 ; i < cur.length ; i ++){
			if ( cur [ i  ] < 16 ) System.out.printf ( " 0%H", cur [ i ] ) ;
			else System.out.printf ( " %H", cur [ i ] ) ;
		}
		System.out.print ( "\n") ; 
	}
}
